﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string strCondition = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["SuperAdmin"] != null && int.Parse(Session["SuperAdmin"].ToString()) > 0)
        {
            if (Session["SuperAdmin"] != "") nPermissions = int.Parse(Session["SuperAdmin"].ToString());
        }
        else
            Response.Redirect("Login.aspx");
        if (!IsPostBack)
        {
        }
    }
    protected void btnBack_Click1(object sender, EventArgs e)
    {
        Response.Redirect("AdminUserList.aspx");
    }
    protected void SubmitImport_Click(object sender, System.EventArgs e)
    {
        CustomerLibrary obj = null;
        HttpPostedFile inpFile = null;
        FileInfo FICSV = null;
        DataColumnCollection tableColumns = null;
        string fileName = Request.Form["txtfileUpLoad"];

        if (Request.Files["txtfileUpLoad"] != null)
        {
            inpFile = Request.Files["txtfileUpLoad"];
        }
        if (txtfileUpLoad.PostedFile.FileName != "")
        {
            FICSV = new FileInfo(txtfileUpLoad.PostedFile.FileName);
            try
            {
                ImportDB.SaveFileToServer(fileName, inpFile);

                DataSet dsCSV = new DataSet();
                dsCSV = ImportDB.ConnectFile(FICSV);

                try
                {
                    tableColumns = dsCSV.Tables[0].Columns;
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                DataRowCollection tableRows = dsCSV.Tables[0].Rows;
                int totalRows = 0;
                int addRows = 0;
                int updateRows = 0;
                GV.Response.Clear();
                GV.Response.Write("<div id='divCurrentRow' style='margin: auto;width: 500px;font-size: 20px;font-weight: bold;color: red;'>");
                GV.Response.Write("Importing Current Row: <span id='spanCurrentRow'></span></div>");
                GV.Response.Flush();
                foreach (DataRow row in tableRows)
                {
                    obj = new CustomerLibrary();
                    object[] rowItems = row.ItemArray;
                    if (rowItems[2].ToString().Trim().Length > 0)
                    {
                        obj.TYPE = CommonFunctions.GetActiveTypeByCodeType(rowItems[0].ToString());
                        try
                        {
                            obj.CREATEDATE = DateTime.Parse(rowItems[1].ToString().Trim());
                        }
                        catch { }
                        obj.CODE = rowItems[2].ToString().Trim();
                        if (obj.CODE.Length == 9 || obj.CODE.Length == 12) obj.CODE = "0" + obj.CODE;
                        obj.NAME = rowItems[3].ToString().Trim();
                        obj.PTNER = rowItems[4].ToString().Trim();
                        Countries objCountry = new Countries();
                        objCountry.Load(rowItems[5].ToString().Replace("'", "").Trim(), GV.cn, GV.trans);
                        obj.COUNTRYCODE = objCountry.CountryName;
                        if (objCountry.CountryName.Length == 0)
                            obj.COUNTRYCODE = rowItems[5].ToString().Trim();
                        obj.ProvinceID = Tinh.GetIDFromCode(obj.CODE.Substring(0, 2), GV.cn, GV.trans);
                        obj.BLNo = rowItems[6].ToString().Trim();
                        obj.VNWRHSE = rowItems[7].ToString().Trim();
                        obj.PODCODE = rowItems[8].ToString().Trim();
                        obj.PODNAME = rowItems[9].ToString().Trim();
                        obj.POLCODE = rowItems[10].ToString().Trim();
                        obj.POLNAME = rowItems[11].ToString().Trim();
                        obj.DLVRY = rowItems[12].ToString().Trim();
                        obj.CURRENCY = rowItems[13].ToString().Trim();
                        try
                        {
                            obj.REVENUE = float.Parse(rowItems[14].ToString().Trim());
                        }
                        catch { }
                        obj.GOODSCODE = rowItems[15].ToString().Trim().Substring(0, 4);
                        obj.GOODSNAME = rowItems[16].ToString().Trim();
                        try
                        {
                            obj.VOLUME = float.Parse(rowItems[17].ToString().Trim());
                        }
                        catch { }
                        obj.UNIT = CommonFunctions.GetUnitNameByCode(rowItems[18].ToString().Trim());
                        obj.MADEIN = rowItems[19].ToString().Trim();
                        obj.ROUTEBY = rowItems[20].ToString().Trim();
                        obj.LASTUPDATE = DateTime.Now;
                        float Revenue = 0, Volume = 0;
                        obj.ID = CustomerLibrary.GetCustLibraryIDImportToLibrary(obj, out Revenue, out Volume, GV.cn, GV.trans);
                        if (obj.ID > 0)
                        {
                            obj.REVENUE += Revenue;
                            obj.VOLUME += Volume;
                            updateRows++;
                        }
                        else
                        {
                            addRows++;
                        }
                        obj.Save(GV.cn, GV.trans);
                        totalRows++;
                        GV.Response.Clear();
                        GV.Response.Write("<script> window.document.getElementById('spanCurrentRow').innerHTML='" + totalRows + "';</script>");
                        GV.Response.Flush();
                    }
                    //Response.Write("Current row:" + totalRows);
                    //Response.Flush();
                }
                strMess = "Total: " + totalRows + " records:<br>";
                if (addRows > 0)
                    strMess += "  - Added New: " + addRows.ToString() + " records.<br>";
                if (updateRows > 0)
                    strMess += "  - Updated: " + updateRows.ToString() + " records.";
                GV.Response.Clear();
                GV.Response.Write("<script> window.document.getElementById('divCurrentRow').innerHTML='DONE!';</script>");
                GV.Response.Flush();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());

            }
        }
        else
        {
            strMess = "Bạn phải chọn file import!";
            return;
        }
    }
}

